package com.atguigu.day08.p4;

/**
 * @version 1.0
 * @Author 马冰
 * @Date 2024/11/8 14:45
 * @注释
 */
public class RectangleTest {
    public static void main(String[] args) {
        //定义一个对象数组接收内容
        Rectangle[] rectangle = getRectangles();

        showInfo("排序前: ", rectangle);

        sort(rectangle);

        showInfo("排序后: ", rectangle);
    }


    private static void sort(Rectangle[] rectangle) {
        for (int i = 0; i < rectangle.length - 1; i++) {
            for (int j = 0; j < rectangle.length - 1; j++) {
                if (rectangle[j].area() > rectangle[j + 1].area()) {
                    Rectangle temp = rectangle[j];
                    rectangle[j] = rectangle[j + 1];
                    rectangle[j + 1] = temp;
                }
            }
        }
    }

    private static void showInfo(String massage, Rectangle[] rectangle) {
        System.out.println(massage);
        for (Rectangle re : rectangle) {
            String info = re.getInfo();
            System.out.println(info);
        }
    }

    private static Rectangle[] getRectangles() {
        Rectangle r = new Rectangle();
        r.setWeight(8);
        r.setWide(2);
        Rectangle r2 = new Rectangle();
        r2.setWeight(7);
        r2.setWide(6);
        Rectangle r3 = new Rectangle();
        r3.setWeight(6);
        r3.setWide(4);
        Rectangle[] rectangle = new Rectangle[3];
        rectangle[0] = r;
        rectangle[1] = r2;
        rectangle[2] = r3;
        return rectangle;
    }
}
